package com.song.email.core;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.song.email.bean.Work;
import com.song.email.util.common.Config;
import com.thoughtworks.xstream.XStream;
@SuppressWarnings({ "rawtypes", "unchecked" })
public class WorkCenter {
	private static WorkCenter workCenter = null;
	private Vector<Work> workList = new Vector<Work>();
	private Log log = LogFactory.getLog(WorkCenter.class);
	private WorkCenter(){}
	
	public synchronized static WorkCenter getInstance(){
		if(null == workCenter){
			workCenter = new WorkCenter();
		}
		return workCenter;
	}
	
	
	public void init(){
		XStream xstream = new XStream();
        xstream.alias("works", Vector.class);  
        xstream.alias("attachList", Vector.class);  
        xstream.alias("work", Work.class);  
        xstream.alias("attach", String.class);  
        String filePath = AuthCenter.class.getResource("/" + Config.workFileName).getPath();
        BufferedReader bReader = null;
        try {
        	bReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath),Config.encoding));
			workList = (Vector)xstream.fromXML(bReader);
		} catch (UnsupportedEncodingException e) {
			log.error("加载works.xml时,字符编码出错.编码:" + Config.encoding,e);
		} catch (FileNotFoundException e) {
			log.error("加载works.xml时,文件未找到.",e);
		}finally{
			if(null != bReader){
				try {
					bReader.close();
				} catch (IOException e) {
					log.error("加载works.xml时,关闭流异常.",e);
				}
			}
		}
	}

	public synchronized Vector<Work> getWorkList() {
		return workList;
	}

	public synchronized int getWorkListSize(){
		return workList.size();
	}
}
